package utils

import (
	"os"
	"time"

	"github.com/google/gopacket"
	"github.com/google/gopacket/layers"
	"github.com/google/gopacket/pcapgo"
	"gitlab.xunlei.cn/xacc/fastlogger"
)

var (
	MTU = 1400
)

var PkgDump = false
var pcapWriter *pcapgo.Writer
var limiter = fastlogger.NewLimiter(5, true)
var logUtils = fastlogger.WithLimiter(limiter).WithField("tun", "utils")

func PcapDump(iPkg []byte) {
	if PkgDump {
		if pcapWriter == nil {
			f, err := os.Create("output.pcap")
			if err != nil {
				logUtils.Errorf("Failed to create pcap file: %v", err)
				return
			}
			pcapWriter = pcapgo.NewWriter(f)
			pcapWriter.WriteFileHeader(65535, layers.LinkTypeIPv4) // 使用IPv4链路类型
		}
		captureInfo := gopacket.CaptureInfo{
			Timestamp:     time.Now(),
			CaptureLength: len(iPkg),
			Length:        len(iPkg),
		}
		if err := pcapWriter.WritePacket(captureInfo, iPkg); err != nil {
			logUtils.Errorf("Failed to write packet to pcap: %v", err)
		}
	}
}

type IpPkgSnmp struct {
	FragmentHeaderNotValid uint64
	HeaderTooShort         uint64
	TcpHeaderTooShort      uint64
	UdpHeaderTooShort      uint64
	IcmpHeaderTooShort     uint64
	NoSessionFound         uint64
	NextHeaderNotSupport   uint64
	SenderNil              uint64
}
